* Open Encuesta CEP 84 Dic 2019 v1: 
use "C:\Users\rdisi\Dropbox\Frontiers\Data\Encuesta CEP 84 Dic 2019 v1.dta"

* add protest variables by comuna
destring COMUNA_BD, replace
* Update codes for comunas un new regions to merge with data using new codes
* Chillán 8401
replace COMUNA_BD=16101 if COMUNA_BD==8401 
* El Carmen 8407
replace COMUNA_BD=16104 if COMUNA_BD==8407 
* Ninhue 8408
replace COMUNA_BD=16204 if COMUNA_BD==8408
* San Carlos 8416
replace COMUNA_BD=16301 if COMUNA_BD==8416
* San Ignacio 8418
replace COMUNA_BD=16108 if COMUNA_BD==8418
* Yungay 8421
replace COMUNA_BD=16109 if COMUNA_BD==8421

merge m:1 COMUNA_BD using "C:\Users\rdisi\Dropbox\Frontiers\Data\protest_comuna_ntile.dta"

* observations not marched from using (228) are the communes no sampled in the 84th CEP survey
* drop not matched from using
drop if _merge==2
tab CERCAVIOLENTA
label variable CERCAVIOLENTA "Nearby violent protests"
sum CERCAVIOLENTA, det

* Variables dependientes
* carabineros
tab ESP_43, nol
gen carabineros=.
replace carabineros=0 if ESP_43==5
replace carabineros=1 if ESP_43==4
replace carabineros=2 if ESP_43==3
replace carabineros=3 if ESP_43==2
replace carabineros=4 if ESP_43==1
* militares
tab ESP_44, nol
gen militares=.
replace militares=0 if ESP_44==5
replace militares=1 if ESP_44==4
replace militares=2 if ESP_44==3
replace militares=3 if ESP_44==2
replace militares=4 if ESP_44==1
label define carabinerosl 0"nunca" 1"casi nunca" 2"a veces" 3"frecuentemente" 4"muy frecuentemente"
label values carabineros carabinerosl
label define militaresl 0"nunca" 1"casi nunca" 2"a veces" 3"frecuentemente" 4"muy frecuentemente"
label values militares militaresl

** Variables independientes 
* Ideología
tab MB_P9
clonevar IDEOLOGÍA_M = MB_P9 if MB_P9<8
tab IDEOLOGÍA_M [aw=POND]

* Educación
tab DS_P4
clonevar educ = DS_P4 if DS_P4<99
tab educ
recode educ (0/3=1) (4=2) (5=3) (7=3) (6=4) (8=4) (9=5)
tab educ [aw=POND]
** etiquetas**
label define educl 1"less than high school" 2 "complete high school" 3"some college" 4 "complete college" 5"graduate and more"
label values educ educl
tab educ [aw=POND]

* Seguridad pública
tab ESP_4
clonevar SEG_PUBLI = ESP_4 if ESP_4<88
label variable SEG_PUBLI "Public order and safety vs. liberty"
sum SEG_PUBLI [aw=POND]

** GSE
gen GSE=DS_P35
tab GSE [aw=POND]

* SEXO
tab DS_P1
clonevar SEXO = DS_P1
tab SEXO [aw=POND]

* Confianza institucional
* Confianza en Carabineros
tab MB_P11_11
clonevar CONFIANZACAR = MB_P11_11 if MB_P11_11<8
tab CONFIANZACAR [aw=POND]
sum CONFIANZACAR [aw=POND], det
* Confianza en FFAA
tab MB_P11_2
clonevar CONFIANZAFFAA= MB_P11_2 if MB_P11_2<8
tab CONFIANZAFFAA [aw=POND]
sum CONFIANZAFFAA [aw=POND], det

* Urban/rural residence
tab ZONA [aw=POND]

* Religion
tab DS_P13
clonevar RE_CAT = DS_P13 if DS_P13<88
tab RE_CAT
generate RELIGION_CAT = RE_CAT
tab RELIGION_CAT
recode RELIGION_CAT (1=1) (2/5=2) (8=3) (9/11=4)
tab RELIGION_CAT
label define RELIGION_CAT 1"catolicos" 2"protestantes" 3"otro" 4"ninguna"
tab RELIGION_CAT
label values RELIGION_CAT RELIGION_CAT
tab RELIGION_CAT [aw=POND]

* Participation in marches during the outburst
gen demonstration =.
replace demonstration=0 if ESP_33==3
replace demonstration=1 if ESP_33==2
replace demonstration=1 if ESP_33==1
label define ldemonstration 1"Demonstrated during the unrest" 0 "Did not demonstrate during the unrest"
label values demonstration ldemonstration

* Perception of insecurity
gen delito=ESP_22_6
replace delito=. if ESP_22_6==88
replace delito=. if ESP_22_6==99
replace delito=0 if ESP_22_6==100
label variable delito "Worried about being victim of crime"
gen worried=ESP_22_6
replace worried=0 if ESP_22_6==100

* Age
gen edad=DS_P2_EXACTA

* Media use
* All (3)
clonevar tv = MB_P13_1 if MB_P13_1 <8
replace tv = 4 - tv 
clonevar read = MB_P13_2 if MB_P13_2 <8
replace read = 4 - read 
clonevar social = MB_P13_3 if MB_P13_3 <8
replace social = 4 - social 
alpha tv read social, item gen(media)
* alpha 0.8001
omegacoef tv read social
* omega 0.8001
label variable media "Media use frequency (all)"
* Traditional (tv y reading)
alpha tv read, item gen(traditional)
label variable traditional "Traditional media use frequency"
label variable social "social media use frequency"
* alpha 0.7545
* omega can't be run with two items

* Authoritarian attitudes ESP_49_1 ESP_49_2 ESP_49_3 ESP_49_4
* Firm government vs people's rights ESP_49_1
clonevar firm_gov=ESP_49_1 if ESP_49_1 <88
* Our country needs a strong leader ESP_49_2
clonevar strong_leader=ESP_49_2 if ESP_49_2 <88
* Obedience and respect for authorities are the most important values that children should learn ESP_49_3
clonevar learn_obedience=ESP_49_3 if ESP_49_3 <88
* Obedience and discipline are they keys to a good life ESP_49_4
clonevar life_obedience=ESP_49_4 if ESP_49_4 <88
* alpha 0.8488
alpha firm_gov strong_leader learn_obedience life_obedience, item gen(authoritarian)
* omega  0.8502
omegacoef firm_gov strong_leader learn_obedience life_obedience
label variable authoritarian "Authoritarian attitudes"

* Analyses
* descriptive: frequency distribution of variables 
tabstat carabineros militares demonstration media traditional social delito CERCAVIOLENTA [aw=POND], st(n mean min max sd sk k q)
* check if variables are normally distributed
* Shapiro-Wilk test
swilk carabineros
swilk militares
* Skewness and kurtosis test 
sktest carabineros [aw=POND]
sktest militares [aw=POND]
* both tests show that the variables 

* Bivariate correlations
* Correlation matrix with Pearson coefficient
* Non-parametric correlations
* Protestar 
tab demonstration carabineros, chi2
tab demonstration militares, chi2

* medios (chi2)
* carabineros 
spearman media carabineros
spearman traditional carabineros
spearman social carabineros
* militares
spearman media militares
spearman traditional militares
spearman social militares

* Fear of crime
* carabineros 
spearman delito carabineros
spearman delito militares

* nearby violent protests
spearman CERCAVIOLENTA carabineros
spearman CERCAVIOLENTA militares

* regressions
**ologit (OR)**
* carabineros (media)
ologit carabineros i.demonstration c.media c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
outreg2 using resultados.xls, replace  eform ctitle(carabineros (media)) alpha(0.001, 0.01, 0.05)
* carabineros (traditional social)
ologit carabineros i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
outreg2 using resultados.xls, append  eform ctitle(carabineros (traditional social)) alpha(0.001, 0.01, 0.05)
* FFAA (media)
ologit militares i.demonstration c.media c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
outreg2 using resultados.xls, append eform ctitle(militares (media)) alpha(0.001, 0.01, 0.05)
* FFAA (traditional social)
ologit militares i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
outreg2 using resultados.xls, append eform ctitle(militares (traditional social)) alpha(0.001, 0.01, 0.05)

**OLS**
* carabineros (media)
reg carabineros i.demonstration c.media c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
outreg2 using resultadosols.xls, replace  ctitle(carabineros (media)) alpha(0.001, 0.01, 0.05)
* carabineros (traditional social)
reg carabineros i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
outreg2 using resultadosols.xls, append  ctitle(carabineros (traditional social)) alpha(0.001, 0.01, 0.05)
* FFAA (media)
reg militares i.demonstration c.media c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
outreg2 using resultadosols.xls, append ctitle(militares (media)) alpha(0.001, 0.01, 0.05)
* FFAA (traditional social)
reg militares i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
outreg2 using resultadosols.xls, append ctitle(militares (traditional social)) alpha(0.001, 0.01, 0.05)

** coefficient plots
* all media
* police (OLR)
ologit carabineros i.demonstration c.media c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
estimates store A
* police (OLS)
reg carabineros i.demonstration c.media c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
estimates store B
* military (OLR)
ologit militares i.demonstration c.media c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
estimates store C
* military (OLS)
reg militares i.demonstration c.media c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
estimates store D
* coefplot
coefplot (A, label(Ordered logistic) msymbol(S)) (B, label(Linear) msymbol(T)), xline(0) keep(*.demonstration media delito CERCAVIOLENTA) mlabel mlabposition(4) mlabgap(*2) bylabel(Police) || (C, label(Ordered logistic) msymbol(S)) (D, label(Linear) msymbol(T)), xline(0) keep(*.demonstration media delito CERCAVIOLENTA) mlabel mlabposition(4) mlabgap(*2) bylabel(Military)

* traditional and social media
* police (OLR)
ologit carabineros i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
estimates store E
* police (OLS)
reg carabineros i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
estimates store F
* military (OLR)
ologit militares i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
estimates store G
* military (OLS)
reg militares i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], vce(rob)
estimates store H
* coefplot
coefplot (E, label(Ordered logistic) msymbol(S)) (F, label(Linear) msymbol(T)), xline(0) keep(*.demonstration traditional social delito CERCAVIOLENTA) mlabel mlabposition(4) mlabgap(*2) bylabel(Police) || (G, label(Ordered logistic) msymbol(S)) (H, label(Linear) msymbol(T)), xline(0) keep(*.demonstration traditional social delito CERCAVIOLENTA) mlabel mlabposition(4) mlabgap(*2) bylabel(Military)


** predictive margins of statistically significant variables
* carabineros
* protests, no demonstration, never social media, not worried about crime
ologit carabineros i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
margins, predict(outcome(4)) at(CERCAVIOLENTA=(0(10)109) demonstration=(0) social=(1) delito=(0)) atmeans post saving(police1, replace)
* protests, demonstrated, never social media, not worried about crime
ologit carabineros i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
margins, predict(outcome(4)) at(CERCAVIOLENTA=(0(10)109) demonstration=(1) social=(1) delito=(0)) atmeans post saving(police2, replace)
* protests, demonstrated, frequently social media, not worried about crime
ologit carabineros i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
margins, predict(outcome(4)) at(CERCAVIOLENTA=(0(10)109) demonstration=(1) social=(3) delito=(0)) atmeans post saving(police3, replace)
* protests, demonstrated, frequently social media, mean crime
ologit carabineros i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZACAR c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
margins, predict(outcome(4)) at(CERCAVIOLENTA=(0(10)109) demonstration=(1) social=(3) delito=(8.242964)) atmeans post saving(police4, replace)
combomarginsplot police1 police2 police3 police4, noci

* militares
* protests, not worried about crime (0)
ologit militares i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA i.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
margins, predict(outcome(4)) at(CERCAVIOLENTA=(0(10)109) delito=(0)) atmeans post saving(military1, replace)
* protests, mean worried about crime
ologit militares i.demonstration c.traditional c.social c.delito c.CERCAVIOLENTA i.IDEOLOGÍA_M i.CONFIANZAFFAA c.authoritarian c.SEG_PUBLI i.GSE i.ZONA ib2.RELIGION_CAT i.educ i.SEXO c.edad [pweight=PONDERADOR], or vce(rob)
margins, predict(outcome(4)) at(CERCAVIOLENTA=(0(10)109) delito=(8.242964)) atmeans post saving(military2, replace)
combomarginsplot military1 military2, noci








